package com.open.capacity.user.controller;

import com.open.capacity.common.model.Title;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.service.TitleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 称号控制层
 * @author liutao
 * @date 2021-02-01 16:32
 */

@RestController
@RequestMapping("/title")
@Api(tags = "称号管理")
public class TiltleController {

    @Autowired
    private TitleService titleService;

    // 模糊查询称号
    @PostMapping("/getSelectTitles")
    @ApiOperation(value = "模糊查询称号")
    public PageResult<Title> getSelectTitles(@RequestParam Map<String, Object> params){
        return titleService.getSelectTitles(params);
    }

    // 添加称号
    @PostMapping("/addTitle")
    @ApiOperation(value = "添加称号")
    public Result addTitle(@RequestBody Title title){
        return titleService.addTitle(title);
    }

    // 修改称号
    @PostMapping("/updateTitle")
    @ApiOperation(value = "修改称号")
    public Result updateTitle(@RequestBody Title title){
        return titleService.updateTitle(title);
    }

    // 删除称号
    @DeleteMapping("/delTitle/{titlid}")
    @ApiOperation(value = "删除称号")
    public Result delTitle(@PathVariable("titlid") int titlid){
        return titleService.delTitle(titlid);
    }

    // 查询自己获得的称号
    @PostMapping("/getTitleByUserId")
    @ApiOperation(value = "查询自己获得的称号")
    public PageResult getTitleByUserId(@RequestParam Map<String, Object> params){
        return titleService.getTitleByUserId(params);
    }

    /**
     * 更新某个教练的称号
     * @param userId
     * @param points
     * @return
     */
    @PutMapping("/updateTitleOfCoachByUserId")
    public Result updateTitleOfCoachByUserId(@RequestParam("userId") Long userId, @RequestParam("points") Double points){
        return titleService.updateTitleOfCoachByUserId(userId,points);
    }

    /**
     * 更新某个班主任的称号
     * @param userId
     * @param points
     * @return
     */
    @PutMapping("/updateTitleOfMasterByUserId")
    public Result updateTitleOfMasterByUserId(@RequestParam("userId") Long userId, @RequestParam("points") Double points){
        return titleService.updateTitleOfMasterByUserId(userId,points);
    }

    /**
     * 更新某个学员的称号
     * @param userId
     * @param points
     * @return
     */
    @PutMapping("/updateTitleOfStudentByUserId")
    public Result updateTitleOfStudentByUserId(@RequestParam("userId") Long userId, @RequestParam("points") Double points){
        return titleService.updateTitleOfStudentByUserId(userId,points);
    }



    /**
     * 更新某个角色的称号
     * @param userId
     * @param points
     * @param roleCode
     * @return
     */
    @PutMapping("/updateTitleOfRoleCodeByUserId")
    public Result updateTitleOfRoleCodeByUserId(@RequestParam("userId") Long userId, @RequestParam("points") Double points,@RequestParam("roleCode")String roleCode){
        return titleService.updateTitleOfRoleCodeByUserId(userId,points,roleCode);
    }


    /**
     * 获取某称号类型的规则
     * @param typeId
     * @param roleId
     * @return
     */
    @GetMapping("/getRuleByTypeAndRoleId")
    public Result getRuleByTypeAndRoleId(@RequestParam("typeId")Integer typeId,@RequestParam("roleId")Integer roleId){
        return titleService.getRuleByTypeAndRoleId(typeId,roleId);
    }


    /**
     * 非角色专属称号更新接口
     * 更新用户的称号，每次获得对应的积分（下载次数增加、上传次数增加、获得点赞数量增加、任务发布量增加、任务采纳量增加）时调用该接口
     *
     * @param userId 需要更新称号的用户ID
     * @param points 该用户的总积分（下载次数、上传次数、获得点赞数量、任务发布量、任务采纳量）
     * @param typeCode 对应的模块CODE，详情查看数据库(task-user-center)的称号类型表格(title_type)中的tp_code列
     * @return
     */
    @PutMapping("/updateTitleByUserIdAndPoints")
        public Result updateTitleByUserIdAndPoints(@RequestParam("userId")Long userId,@RequestParam("points") Double points, @RequestParam("typeCode")String typeCode){
            return titleService.updateTitleByUserIdAndPoints(userId,points,typeCode);
        }

}
